<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript" src="js/common.js"></script>
<script>
var fileAsText;
var fileExt;

var countResults = function(data) {
	var count = 0;
	for (var k in data) {
		if (data.hasOwnProperty(k)) {
		   ++count;
		}
	}
	return count;
}
var openPixlr = function (info) {
	var params = {
		'image': info.srcUrl,
		'target': chrome.extension.getURL('target.html'),
		'locktarget': true
	}
	var url = 'http://pixlr.com/editor/?' + $.param(params);
	chrome.tabs.create({"url":url});
};

var saveToHyves = function (info) {
	var i = info.menuItemId - info.parentMenuItemId - 1;
    if(localStorage['hyves_albums']) {
        var retrievedObject = localStorage['hyves_albums'];			
        var oAlbums = JSON.parse(retrievedObject);
        var album = oAlbums.album[i];
        var albumid = album.albumid; 
    } else {
        var albumid = 0;
    }
	var aUrlParts = info.srcUrl.split('/')
	var fileName = aUrlParts[aUrlParts.length - 1];
	var params = {
		'image': info.srcUrl,
		'title': fileName,
		'albumid': albumid
	}
	var url = chrome.extension.getURL('target.html') + '?' + $.param(params);
	chrome.tabs.create({"url":url});
};

var setContextmenu = function () {
	chrome.contextMenus.removeAll();
    if(localStorage["hyves_setting_contextmenu"] != 0) {
        var pixlritem = chrome.contextMenus.create({
            "type": "normal",
            "title": "Edit eerst met Pixlr",
            "contexts": ["image"],
            "onclick": openPixlr
        });
        if (!localStorage['hyves_default_albumid'] || localStorage['hyves_default_albumid'] == 0) {
            if(localStorage['hyves_albums']) {
                var retrievedObject = localStorage['hyves_albums'];			
                var oAlbums = JSON.parse(retrievedObject);
                var hyvesitem = chrome.contextMenus.create({
                    "type": "normal",
                    "title": "Upload direct naar:",
                    "contexts": ["image"],
                    "onclick": saveToHyves
                });
                for(i = 0; i < countResults(oAlbums.album); i++) {
                    var album = oAlbums.album[i];
					var sAlbum = album.title.replace(/\&amp;/g,' en ') + ' (' + albumnames[album.visibility] + ') ';
                    chrome.contextMenus.create({
                        "type": "normal",
                        "title": sAlbum,
                        "parentId": hyvesitem,
                        "contexts": ["image"],
                        "onclick": saveToHyves
                    });
                }
                chrome.contextMenus.create({
                    "type": "normal",
                    "title": "Al mijn foto's (geen album)",
                    "parentId": hyvesitem,
                    "contexts": ["image"],
                    "onclick": saveToHyves
                });						
            } else {
                var hyvesitem = chrome.contextMenus.create({
                    "type": "normal",
                    "title": "Upload direct",
                    "contexts": ["image"],
                    "onclick": saveToHyves
                });
            }
        }
    }
}
chrome.extension.onConnect.addListener(function(port) {
	port.onMessage.addListener(function(info) {
			if(info.name == 'refresh_contextmenu') {
				setContextmenu();
			}
	});
});
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
	if (request.type == 'file') {
		fileAsText = request.fileAsText;
		var aFileNameParts = request.fileName.split('.');
		fileExt = aFileNameParts[aFileNameParts.length - 1];
		var params = {
			'fileFromBackgroundPage': true,
			'title': request.fileName,
			'tabId': sender.tab.id
		};
	} else {
		var params = {
			'image': request.url,
			'title': request.title,
			'tabId': sender.tab.id
		};
	}
	var url = chrome.extension.getURL('target.html') + '?' + $.param(params);	  
	console.log(url);
	chrome.tabs.create({}, function (tab) {
		chrome.tabs.update(tab.id, {url:url});
		sendResponse({success: 'jippie'});
	});
});
setContextmenu();
</script>